package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.IProductPeiJizhongService;
import com.cn.kehong.api.IProductPeiTouService;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/jizhong")
public class ProductPeiJizhongController {

    @Autowired
    private IProductPeiJizhongService iProductPeiJizhongService;

    //批次投料明细
    @RequestMapping("/getJizhongList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiJizhongVo>> getProductPeiTouList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiJizhongVo> > response = new JsonResponse<List<ProductPeiJizhongVo> >();
        List<ProductPeiJizhongVo> lists = iProductPeiJizhongService.getJizhongList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //单日投料汇总
    @RequestMapping("/getJizhongListByDay/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiJizhongVo>> getJizhongListByDay(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiJizhongVo> > response = new JsonResponse<List<ProductPeiJizhongVo> >();
        List<ProductPeiJizhongVo> lists = iProductPeiJizhongService.getJizhongListByDay(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //批次投料明细-下载
    @ResponseBody
    @RequestMapping("/exportJizhongList")
    public void exportJizhongList(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setFactoryName(jsonObject.getString("factoryName"));
            queryForm.setBanCi(jsonObject.getString("banCi"));
            queryForm.setGradeValue(jsonObject.getString("gradeValue"));

        }
        iProductPeiJizhongService.exportJizhongList(queryForm,response);
    }


    //单日投料汇总-下载
    @ResponseBody
    @RequestMapping("/exportJizhongListByDay")
    public void exportJizhongListByDay(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setFactoryName(jsonObject.getString("factoryName"));

        }
        iProductPeiJizhongService.exportJizhongListByDay(queryForm,response);
    }


    //单日投料汇总-查看
    @RequestMapping("/getByIds/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getByIds(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  voList = iProductPeiJizhongService.getByIds(queryForm);
        response.setResult(voList);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getProductPeiJizhongListByPeiId/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiJizhong>> getProductPeiJizhongListByPeiId(String peiId, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiJizhong> > response = new JsonResponse<List<ProductPeiJizhong> >();
        List<ProductPeiJizhong> lists = iProductPeiJizhongService.getProductPeiJizhongListByPeiId(peiId);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }




    @ResponseBody
    @RequestMapping("/addBatchInsertProductPeiJizhongs/{frontToken}")
    public JsonResponse<String> addBatchInsertProductPeiJizhongs(@RequestBody List<ProductPeiJizhong> productPeiJizhongs, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = iProductPeiJizhongService.addBatchInsertProductPeiJizhongs(productPeiJizhongs);
        return CommonUtil.getResponseData(response, result);
    }
    //单日投料汇总(首页)
    @RequestMapping("/getJizhongListByHome/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getJizhongListByHome(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  lists = iProductPeiJizhongService.getJizhongListByHome(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }
}
